﻿<cfscript>
/** 
* ORM FormClass
* 
* @hint "Hibernate persistenter t_class. 定义了有关（行政）班级的基本数据项" 
* 
*/ 

component 
	displayname="public.service.orm.senate.FormClass" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="FormClass" 
	table="t_class"
	{

	property type="string" name="ClassID" column="cls_id" fieldtype="id" generator="assigned" hint="班号";

	property type="string" name="Name" column="cls_name" hint="班级";
	property type="string" name="ClassFound" column="cls_found" hint="建班年月";

	property type="numeric" name="PlanSize" column="cls_size" hint="班级计划人数";
	property type="numeric" name="RealSize" column="cls_scale" hint="班级人数";

	property type="string" name="TeacherInCharge" column="teacher_in_charge" hint="班主任教工号";
	property type="string" name="Monitor" column="classmonitor" hint="班长学号";
	property type="string" name="Assistant" column="assistant" hint="辅导员号";

	property type="string" name="Grade" column="grade" hint="年级";

	property name="Subject" fieldtype="many-to-one" fkcolumn="sbj_id" cfc="Subject"  hint="专业";
	property name="Campus" fieldtype="many-to-one" fkcolumn="campus_id" cfc="Campus"  hint="所在校区";
	
	property name="Books" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cls_id" cfc="FormClassBook" hint="班级教材征订计划";
	property name="BookStocks" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cls_id" cfc="BookStockOutClass" hint="教材按行政班出库清单";
	property name="ProgramPlans" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cls_id" cfc="ProgramPlan" hint="开课计划";
	property name="Students" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cls_id" cfc="Student" hint="行政班学生";
	
	/**
	* @hint "自定义主键规则"
	* 
	* @Subject 专业
	* @Grade 年级
	*/
	public string function getNextID( required Subject, required string Grade ) output=false {
		
		var nextID = left(arguments.Subject.getSubjectID() & "0000", 6) & right(arguments.Grade, 2) & numberFormat(1, "0_");

		var checkArray = ORMExecuteQuery("select ClassID from FormClass 
											where 
											Subject = :subject 
											and
											Grade = :grade
											and
											ClassID like :condition
											order by ClassID desc", {subject = arguments.Subject, grade = arguments.Grade, condition = left(arguments.Subject.getSubjectID() & "0000", 6) & right(arguments.Grade, 2) & "__" }, { maxResults=1 });
		/* 
			主键生成规则 
			
			班级代码为10位 前6位为专业(或专业方向代码) +  年级代码后2位 + 2位流水号
			
			若班级是划分至专业 则前5位专业代码 + 0
			若班级是划分至专业方向 则前6位专业方向代码
		*/
		
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = left(arguments.Subject.getSubjectID() & "0000", 6) & right(arguments.Grade, 2) & numberFormat((right(checkArray[1], 2) + 1), "0_");
			
		}
		
		return nextID;
	}
}
</cfscript>
